package org.yaac.shared.editor;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.yaac.shared.property.KeyInfo;
import org.yaac.shared.property.PropertyInfo;

import com.google.gwt.view.client.ProvidesKey;

/**
 * entity info used by browser's view model
 * 
 * @author Max Zhu (thebbsky@gmail.com)
 * 
 */
public class EntityInfo implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * The key provider that provides the unique ID of a contact.
	 */
	public static final ProvidesKey<EntityInfo> KEY_PROVIDER = new ProvidesKey<EntityInfo>() {
		public Object getKey(EntityInfo item) {
			return item == null ? null : item.getKey();
		}
	};

	private KeyInfo key;

	private Map<String, PropertyInfo> propertisMap;
	
	/**
	 * used by GWT
	 */
	@SuppressWarnings("unused")
	private EntityInfo() {
	}

	public EntityInfo(KeyInfo key) {
		super();
		this.key = key;
		this.propertisMap = new HashMap<String, PropertyInfo>();
	}

	public KeyInfo getKey() {
		return key;
	}
	
	public Map<String, PropertyInfo> getPropertisMap() {
		return propertisMap;
	}

	public String propertyAsHtml(String propertyName) {
		PropertyInfo property = propertisMap.get(propertyName);
		
		return property == null ? "<br/>" : property.asHtml();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((key == null) ? 0 : key.hashCode());
		result = prime * result + ((propertisMap == null) ? 0 : propertisMap.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		EntityInfo other = (EntityInfo) obj;
		if (key == null) {
			if (other.key != null)
				return false;
		} else if (!key.equals(other.key))
			return false;
		if (propertisMap == null) {
			if (other.propertisMap != null)
				return false;
		} else if (!propertisMap.equals(other.propertisMap))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "EntityInfo [key=" + key + ", propertisMap=" + propertisMap + "]";
	}
}
